//
//  CheckReachability.m
//  iPhoneDev02
//
//  Created by smu on 11-7-17.
//  Copyright 2011年 duk. All rights reserved.
//

#import "CheckReachability.h"
#import "Reachability.h"

@implementation CheckReachability


- (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired = [curReach connectionRequired];
    NSString* statusString = @"";
    switch (netStatus) {
        case NotReachable:
        {
            statusString = @"Access Not Available";
            imageView.image = [UIImage imageNamed:@"stop-32.png"];
            //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
            connectionRequired = NO;
            break;
        }
        case ReachableViaWWAN:
        {
            statusString = @"Reachable WMAN";
            imageView.image = [UIImage imageNamed:@"WWAN5.png"];
            break;
        }
        case ReachableViaWiFi:
        {
            statusString = @"Reachable WiFi";
            imageView.image = [UIImage imageNamed:@"Airport.png"];
            break;
        }
    }
    if (connectionRequired) {
        statusString = [NSString stringWithFormat:@"%@, Connection Required", statusString];
    }
    textField.text = statusString;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    if (curReach == hostReach) {
        [self configureTextField:remoteHostStatusField imageView:remoteHostIcon reachability:curReach];
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        BOOL connectionRequired = [curReach connectionRequired];
        
        summaryLabel.hidden = (netStatus != ReachableViaWWAN);
        NSString* baseLabel = @"";
        if (connectionRequired) {
            baseLabel = @"Cellular data network is available.\n  Internet traffic will be routed through it after a connection is established.";
        } else {
            baseLabel = @"Cellular data network is active.\n  Internet traffic will be routed through it.";
        }
        summaryLabel.text = baseLabel;
    }
    if (curReach == internetReach) {
        [self configureTextField:internetConnetionStatusField imageView:internetConnectionIcon reachability:curReach];
    }
    if (curReach == wifiReach) {
        [self configureTextField:localWiFiConnectionStatusField imageView:localWiFiConnectionIcon reachability:curReach];
    }
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self updateInterfaceWithReachability:curReach]; 
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    #pragma  unsed(application)
    contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    summaryLabel.hidden = YES;
    
    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called. 
    //// 监测网络情况
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    //Change the host name here to change the server your monitoring
    remoteHostLabel.text = [NSString stringWithFormat:@"Remote Host: %@", @"www.apple.com"];
    hostReach = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain];
    [hostReach startNotifier];
    [self updateInterfaceWithReachability:hostReach];
    
    internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [internetReach startNotifier];
    [self updateInterfaceWithReachability:internetReach];
    
    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];
    [self updateInterfaceWithReachability:wifiReach];
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
